package cn.jiedh.app.meiweidk.Utils.tool;

import android.content.ClipboardManager;
import android.content.Context;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.widget.TextView;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @name：
 * @author： 周毅
 * @phone： 15826080712
 * @createTime： 2017/7/31
 * @modifyTime： 2017/7/31
 * @explain：
 */

public class NumUtils {
    /**
     * 判断手机号格式
     *
     * @param mobiles
     * @return
     */
    public static boolean isMobileNO(String mobiles) {

        Pattern p = Pattern.compile("^((17[0-9])|(13[0-9])|(15[0-9])|(18[0-9]))\\d{8}$");

        Matcher m = p.matcher(mobiles);

        return m.matches();

    }

    /**
     * 判断密码格式
     *
     * @param //email
     */
    public static boolean isPassword(String password) {
        // Pattern p = Pattern.compile("(?!^\\d+$)(?!^[a-zA-Z]+$)(?!^[_#@]+$).{6,20}");
        Pattern p = Pattern.compile("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$");
        Matcher pm = p.matcher(password);
        if (pm.matches()) {
            return true;
        }
        return false;
    }

    /**
     * @param monthlyrate 月利息
     * @param monthnumber 期限
     * @param money       借款金额
     * @return 每月还款金额
     */
    public static String MothRepays(double money, double monthlyrate, int monthnumber) {
        double moneys = money * monthlyrate *
                Math.pow((1 + monthlyrate), monthnumber) / (Math.pow(1 + monthlyrate, monthnumber) - 1);
        return doubleFormateStr(moneys);
    }

    /**
     * 总金额
     *
     * @param money
     * @param monthlyrate
     * @param monthnumber
     * @return
     */
    public static String Allmoney(double money, double monthlyrate, int monthnumber, int num) {
        double moneys = money * monthlyrate *
                Math.pow((1 + monthlyrate), monthnumber) / (Math.pow(1 + monthlyrate, monthnumber) - 1);

        return doubleFormateStr(moneys * num);
    }

    public static String doubleFormateStr(double str) {
        NumberFormat nf = NumberFormat.getNumberInstance();
        nf.setMaximumFractionDigits(2);
        return nf.format(str);
    }

    /**
     * 手机号码后6位处理
     *
     * @param pNumber
     * @return
     */
    public static String getphone(String pNumber) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < pNumber.length(); i++) {
            char c = pNumber.charAt(i);
            if (i >= 3 && i <= 6) {
                sb.append('*');
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    /**
     * 设置字符串某些字体的颜色
     *
     * @param str
     * @param ch1
     * @param ch2
     * @param color
     * @param tv
     */
    public static void setTVColor(String str, char ch1, char ch2, int color, TextView tv) {
        int a = str.indexOf(ch1) + 1; //从字符ch1的下标开始
        int b = str.indexOf(ch2); //到字符ch2的下标+1结束,因为SpannableStringBuilder的setSpan方法中区间为[ a,b )左闭右开
        SpannableStringBuilder builder = new SpannableStringBuilder(str);
        builder.setSpan(new ForegroundColorSpan(color), a, b, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv.setText(builder);
    }

    /**
     * 计算剩余日期
     *
     * @param
     * @return
     */
    public static String calculationRemainTime(String endTime, long countDown) {

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date now = new Date(System.currentTimeMillis());// 获取当前时间
            Date endData = df.parse(endTime);
            long l = endData.getTime() - countDown - now.getTime();
            long day = l / (24 * 60 * 60 * 1000);
            long hour = (l / (60 * 60 * 1000) - day * 24);
            long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
            long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
            return "剩余" + day + "天" + hour + "小时" + min + "分" + s + "秒";
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "";
    }

    //根据身份证号输出年龄
    public static int IdNOToAge(String IdNO) {
        int leh = IdNO.length();
        String dates = "";
        if (leh == 18) {
            int se = Integer.valueOf(IdNO.substring(leh - 1)) % 2;
            dates = IdNO.substring(6, 10);
            SimpleDateFormat df = new SimpleDateFormat("yyyy");
            String year = df.format(new Date());
            int u = Integer.parseInt(year) - Integer.parseInt(dates);
            return u;
        } else {
            dates = IdNO.substring(6, 8);
            return Integer.parseInt(dates);
        }
    }

    //复制内容到系统剪切板
    public static void copystr(Context context, String str) {
        ClipboardManager cm = (ClipboardManager) context.getApplicationContext().getSystemService(Context.CLIPBOARD_SERVICE);
        // 将文本内容放到系统剪贴板里。
        cm.setText(str);
        T.showShortToast("已复制到剪切板");
    }

    //保留两位小数
    public static String getporm2(int str) {

        double str1 = Double.valueOf(str);
        DecimalFormat decimalFormat = new DecimalFormat("#.00");
        String sstr1 = decimalFormat.format(str1 / 10000);
        return sstr1;
    }

    /**
     * 保留小数点后两位
     *
     * @param num 需要保留小数点后两位的数据
     */
    public static String getTwoDecimal(double num) {
        DecimalFormat df = new DecimalFormat("######0.00");
        return df.format(num);
    }

    /**
     * 将数字以逗号隔开123，456
     *
     * @param str
     * @return
     */
    public static String addComma(String str) {
// 将传进数字反转
        String reverseStr = new StringBuilder(str).reverse().toString();
        String strTemp = "";
        for (int i = 0; i < reverseStr.length(); i++) {
            if (i * 3 + 3 > reverseStr.length()) {
                strTemp += reverseStr.substring(i * 3, reverseStr.length());
                break;
            }
            strTemp += reverseStr.substring(i * 3, i * 3 + 3) + ",";
        }
// 将[789,456,] 中最后一个[,]去除
        if (strTemp.endsWith(",")) {
            strTemp = strTemp.substring(0, strTemp.length() - 1);
        }
// 将数字重新反转
        String resultStr = new StringBuilder(strTemp).reverse().toString();
        return resultStr;
    }

    /**
     * 获取格式化的数据，类似：454,464
     *
     * @param num
     * @return
     */
    public static String getDivideNum(String num) {
        if (num != null) {
            if (num.length() > 3) {
                return getDivideNum(num.substring(0, num.length() - 3)) + "," + num.substring(num.length() - 3, num.length());
            } else {
                return num;
            }
        } else {
            return "";
        }
    }
}
